const {connection,mysqlDao} = require('../utils/jdbc-connection')
const BaseDao = require("./base-dao");

/**
 * 博客操作
 */
class LeacotsDao extends BaseDao{
    constructor() {
        super();
    }

    /**
     * 分页查询留言索引
     * @param pageIndex：页码
     * @param pageSize：每页显示条数
     * @param options:条件
     * @return arr[{id,title,content}]
     */
    getLeacotsList(pageIndex = 1,pageSize = 20,options = {}){
        const start = pageIndex - 1
        let sql = 'SELECT a.id,a.content,a.create_time,a.user_id,b.avatar,b.nickname FROM leacots a,user b where a.user_id = b.id order by create_time desc limit ?,? '
        return connection(sql,[start*pageSize,pageIndex*pageSize])
    }


    /**
     * 插入一条留言，并返回主键
     * @param leacots
     * @returns {{}}
     */
    insertLeacots(leacots) {
        const {content,createTime,userId} = leacots
        let sql = 'insert into leacots(content,create_time,user_id) values(?,?,?);'
        return connection(sql,[content,createTime,userId])
    }

    /**
     * 更新留言
     * @param leacots
     * @returns {Promise<unknown>}
     */
    updateLeacots(leacots) {
        const {id,content,createTime,userId} = leacots
        let sql = 'update leacots set content = ?, create_time =?  where id = ?'
        return connection(sql,[content,createTime,id])
    }

}

module.exports = LeacotsDao;
